﻿using Dtos;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace _2022_2_28.Controllers
{
    public class DepartmentController : Controller
    {
        private IDepartmentBll _bll;
        public DepartmentController(IDepartmentBll bll)
        {
            _bll = bll;
        }
        public async Task<IActionResult> Index()
        {
            var data = await _bll.GetAll();
            return View(data);
        }

        [HttpGet]
        public async Task<IActionResult> Add() 
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Add(string title) 
        {
            var data = await _bll.AddDepartmentAsync(title);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public async Task<IActionResult> Update(Guid id) 
        {
            var data = await _bll.GetDepartmenByIdAsync(id);
            return View(data);
        }

        public async Task<IActionResult> Update(DepartmentDto d) 
        {
            var data = await _bll.UpdateDepartmenAsync(d.Id,d.Title,d.UpdateTime);
            return RedirectToAction("Index");
        }

        public async Task<IActionResult> Delete(Guid id) 
        {
            var data = await _bll.DeleteDepartmenAsync(id);
            return RedirectToAction("Index");
        }

    }
}
